<?php
 
	function smarty_block_income($params, $content, &$smarty, &$repeat) {

		global $db, $config, $counter;

		$l = count($smarty->_tag_stack);
		
		
		if(empty($params['name'])) $params['name'] = "income";

		if(empty($smarty->blockvars[$l])) {
			$sqlstr = "select a.*,b.pname as product_name,b.spec,b.pid,b.pic,c.title as supplier_name,d.title as paper_name,d.shopid as shop_id,d.supplierid as supplier_id,e.title as shop_name,f.barcode from {$config->db_prefix}purchases as a left join haiou_product_sku as b on a.productid=b.id left join {$config->db_prefix}purchases_paper as d on a.paper_id=d.id left join {$config->db_prefix}suppliers as c on d.supplierid=c.id left join {$config->db_prefix}shops as e on d.shopid=e.id left join {$config->db_prefix}products as f on a.productid=f.product_sku_id where (d.status=2 or d.status=9 or d.status is null)";
			if (!empty($params['id'])) $sqlstr .= " and a.id = ".$db->qstr($params['id']);
			if (!empty($params['product_id'])) $sqlstr .= " and b.pid in(".trim($params['product_id']).")";
			if (!empty($params['shopid'])) $sqlstr .= " and a.shopid = ".$db->qstr($params['shopid']);
			if (!empty($params['keywords'])) $sqlstr .= " and b.pname like ". $db->qstr("%".$params['keywords']."%");
			if (!empty($params['paper_name'])) $sqlstr .= " and d.title like ". $db->qstr("%".$params['paper_name']."%");
			if (!empty($params['sdate'])) $sqlstr .= " and a.thedate>=". strtotime($params['sdate']);
			if (!empty($params['edate'])) $sqlstr .= " and a.thedate<=". (strtotime($params['edate'])+86399);
			if (!empty($params['lprice'])) $sqlstr .= " and a.pricebuy>=". $db->qstr($params['lprice']);
			if (!empty($params['hprice'])) $sqlstr .= " and a.pricebuy<=". $db->qstr($params['hprice']);
			if (!empty($params['lnumber'])) $sqlstr .= " and a.number>=". $db->qstr($params['lnumber']);
			if (!empty($params['hnumber'])) $sqlstr .= " and a.number<=". $db->qstr($params['hnumber']);
			
			if (!empty($params['supplier'])) $sqlstr .= " and c.title like ". $db->qstr("%".$params['supplier']."%");
				
			if (!empty($params['lotnumber'])) $sqlstr .= " and a.lotnumber like ". $db->qstr("%".$params['lotnumber']."%");
			if (!empty($params['barcode'])) $sqlstr .= " and f.barcode=".$db->qstr($params['barcode']);
			if (!empty($params['sdate1'])) $sqlstr .= " and a.timeexpress>=". strtotime($params['sdate1']);
			if (!empty($params['edate1'])) $sqlstr .= " and a.timeexpress<=". strtotime($params['edate1']);
			if (!empty($params['sdate2'])) $sqlstr .= " and a.timeend>=". strtotime($params['sdate2']);
			if (!empty($params['edate2'])) $sqlstr .= " and a.timeend<=". (strtotime($params['edate2'])+86399);
			if (!empty($params['sdate3'])) $sqlstr .= " and a.timestocked>=". strtotime($params['sdate3']);
			if (!empty($params['edate3'])) $sqlstr .= " and a.timestocked<=". strtotime($params['edate3']);
				
			
			if (!empty($params['cond'])) $sqlstr .= " and ({$params['cond']})";
			
			if($params['type']!==null && $params['type']!==''){
			     $sqlstr .= " and a.type= {$params['type']}";
			}
			if($params['status']==''){
				$params['status'] = '0';
			}
			if($params['status']==="0"){
			    $sqlstr .= " and a.timestocked= 0";
			    $params['order'] = "(IF(a.timeexpress>0,1,0)) DESC";
			}elseif ($params['status']==="1"){
			    $sqlstr .= " and a.timestocked> 0";
			}elseif ($params['status']==="-1"){
			    $sqlstr .= " and a.timestocked= -1";
			}
				
			
			if (!empty($params['counter'])) $counter = $db->getrow(str_replace("a.*", "sum(number) number, sum(pricebuy*number) price", $sqlstr));
			if (!empty($params['order'])) $sqlstr .= " order by {$params['order']}";
			
			

			$limit = empty($params['limit'])?9999:$params['limit'];
			$page = empty($params['page'])?(empty($_GET['list_page'])?1:$_GET['list_page']):$params['page'];
			$pager = new pager($limit, $page);
			
// 			echo $sqlstr;
			$smarty->blockvars[$l] = $pager->execute($db, $sqlstr);
			//取sku第一张图
			foreach ($smarty->blockvars[$l] as $kk=>$vv)
			{
				if(!($smarty->blockvars[$l][$kk]['product_pic'] = @strstr($vv['pic'], ",",true)))
				{
					$smarty->blockvars[$l][$kk]['product_pic'] = $vv['pic'];
				}
			}
// 			print_r($smarty->blockvars[$l]);exit;
			if(!$smarty->blockvars[$l]) return $repeat = false;
			if(!empty($params['pager'])) $smarty->assign($params['pager'], $pager);
		}
		if(list($key, $item) = each($smarty->blockvars[$l])) {
			
			$item['type']=$config->product_type[$item['type']];
			$item['usedby']=intval($item['usedby']);
			$repeat = true;
			if(!empty($counter)) $item["counter"] = $counter;
			$smarty->assign($params['name'], $item);
		}
		else {
			$repeat = false;
			$counter = "";
			reset($smarty->blockvars[$l]);
		}
		
		if(!is_null($content)) print $content;
	
		//
		if(!$repeat) $smarty->blockvars[$l] = array();
	}

?>